// 封装axios 导出request
import axios from 'axios'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

const request = axios.create({
  baseURL: 'http://dida100.com:8888',
  timeout: 5000
})
request.interceptors.request.use(config => {
  NProgress.start()
  config.headers['Authorization'] = "Bearer "+window.sessionStorage.getItem('token')
  return config
})
request.interceptors.response.use(req => {
  NProgress.done()
  // 统一处理错误 401 403 404 500
  if (req.data.code === 401) {
    window.sessionStorage.removeItem('token')
    window.location.href = '/login'
  }
  if (req.data.code === 403) {
    window.location.href = '/403'
  }
  if (req.data.code === 404) { 
    window.location.href = '/404'
  }
  if (req.data.code === 500) {
    window.location.href = '/500'
  }

  return req
}, error => {
  // 错误提示使用antd的message
   
  NProgress.done()
  return Promise.reject(error)
})
export default request